#!/usr/bin/env python

#Name: pyrev.py
#Version: 1.3
#Author: LeXeL and s3my0n
#Website: InterN0T.net

#########################################################################
# PyRev takes an IP address or domain name and prints a list of domains #
# hosted on that IP address.                                            #
#                                                                       #
# Copyright (C) 2010 LeXeL, s3my0n                                      #
#                                                                       #
# This program is free software: you can redistribute it and/or modify  #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or     #
# any later version.                                                    #
#                                                                       #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################

import urllib2, sys
from socket import gethostbyname
from re import findall

class ReverseIP(object):
      def __init__(self, server='http://www.ip-adress.com/reverse_ip/'):
	    self.server = server
	    self.ua = 'Mozilla/5.0 (compatible; Konqueror/3.5.8; Linux)'
	    self.h = {'User-Agent': self.ua}
			
      def title(self):
	    t= '''
\t    ______      ______ _____ _   _ 
\t    | ___ \     | ___ \  ___| | | |
\t    | |_/ /_   _| |_/ / |__ | | | |
\t    |  __/| | | |    /|  __|| | | |
\t    | |   | |_| | |\ \| |___\ \_/ /
\t    \_|    \__, \_| \_\____/ \___/ 
\t             _/ |                  
\t            |___/
\t
\t LeXeL  lexelEZ[at]gmail[dot]com
\t s3my0n RuSH4ck3R[at]gmail[dot]com'''
	    return t

      def url(self):
	    try:
                  r = urllib2.Request('%s%s' % (self.server, self.site), headers=self.h)
		  f = urllib2.urlopen(r)
	    except BaseException, e:
		  print '\n [-] Error: %s' % (e)
		  sys.exit(1)
	    else:
		  self.source = f.read()
      def getip(self):
	    try:
                  ip = gethostbyname(self.site)
	    except BaseException, e:
		  print '\n [-] Error: %s ' %(e)
	    else:      
		  print '\t\n [*] Scanning %s\n' %(ip)
			
      def whoami(self):
	    found = findall("href=\"/whois/\S+\">Whois</a>]", self.source)
	    writeable = True
	    for i in found:
		  i = i.replace('href=\"/whois/', '')
		  i = i.replace('\">Whois</a>]', '')
		  if writeable:
			try:
                              outp = open(self.fileout, 'a+')
			      outp.write('%s\n' % (i))
			      outp.close()
			except IOError:
			      print ' [-] Failed writing to (%s)' % (self.fileout)
			      print ' [*] Continuing without writing logfile\n'
			      writeable = False
				  
                  print '\t%s ' % (i)
				  
def getinput():
      try:
	    site = raw_input('\nEnter site to scan: ')
	    fileout = raw_input('\nEnter logfile name: ')
      except (KeyboardInterrupt, IOError):
	    print '\n\n [*] Error: Aborted'
	    sys.exit(1)
      else:
	    return (site, fileout)

if __name__ == '__main__':
      # starting
      p = ReverseIP()
      print p.title()
      # getting user input
      userinput = getinput()
      p.site = userinput[0]
      p.fileout = userinput[1]
      # getting info
      p.url()
      p.getip()
      p.whoami()
